﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YAPL.Language.Ast.Utils {
	/// <summary>
	/// Marks descendant nodes of abstract syntax tree
	/// </summary>
	[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
	public class DescendantAttribute : Attribute {
		private readonly AstVisit _visit;
		public DescendantAttribute(AstVisit visit) {
			_visit = visit;
			Priority = 0;
		}

		/// <summary>
		/// Determines wether descendant node is visited before or after current
		/// </summary>
		public AstVisit Visit {
			get { return _visit; }
		}

		/// <summary>
		/// Higher value means that descendant will be reviewed earlier
		/// </summary>
		public int Priority { get; set; }
	}

	/// <summary>
	/// Class for comparing DescendantAttribute instances according to walk order priority
	/// </summary>
	public class DescendantAttributePriorityComparer : IComparer<DescendantAttribute> {
		public int Compare(DescendantAttribute x, DescendantAttribute y) {
			var xPriority = x.Priority;
			var yPriority = y.Priority;
			return yPriority.CompareTo(xPriority);
		}
	}
}
